package com.controller;

import com.aop.SystemLog;
import com.aop.SystemMail;
import com.dto.BaseDto;
import com.entity.Condition;
import com.entity.Comment;
import com.page.PageInfo;
import com.service.CommentService;
import com.utils.TimeDateUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.text.ParseException;
import java.util.List;
import java.util.Optional;

@RestController
@RequestMapping("/commentController")
public class CommentController extends BaseController {
   @Resource
    CommentService commentService;




    //根据主键查询,可以使用占位符来声明变量{}占位 mid就是参数值,通过 @PathVariable 绑定指定变量赋值
    @GetMapping("/comment/{cmpid}")
    public BaseDto getCommentByCmpid(@PathVariable("cmpid") Integer cmpid)throws Exception{
        System.out.println(cmpid);
        BaseDto dto = commentService.getCommentByCmpid(cmpid);


        return dto;
    }

    @GetMapping("/comments/page")
    public ModelAndView getCommentsByPage(PageInfo pageInfo) {
        String cacheId = "CommentsPage"+pageInfo.getCurrPage();
        BaseDto dto = commentService.getCommentsByPage(pageInfo,cacheId);
        //下一面一句等价于 request.getDispatcherRequest.forward + request.setAttibute("dto",dto)
        ModelAndView modelAndView = new ModelAndView("/comment/list", "dto", dto);
        modelAndView.addObject("pageInfo", pageInfo);

        return modelAndView;
    }




    //@RequestMapping(value="/comment",method = RequestMethod.POST)
    //方法 url名字相同 但请求方式不同 用不通电的方法来相应  restful
    @PostMapping("/comment")
    @ResponseBody
    @SystemMail(op = "Comment:reg",queueName = "lg.user.reg",module = "会员管理",methods = "会员注册")
    public BaseDto add(@Valid Comment comment) throws Exception {
        return commentService.add(comment) ;
      }
    @PutMapping("/comment")
    @ResponseBody
    public BaseDto update(Comment comment)throws Exception {
        System.out.println("1111");
        return commentService.update(comment) ;
    }

    //@RequestMapping(value="/comment",method = RequestMethod.GET)
    @GetMapping("/comment")
    @ResponseBody
    public String getById(){
        return "getById1";
    }
    //get 查询  post add  put 修改  patch 部分修改   delete 删除

    @DeleteMapping("/comment")
    @ResponseBody
    public String delete(){return "delete";}
    @PatchMapping("/comment")
    @ResponseBody
    public String patch(){return "patch";}


}
